# Copyright (c) 2024 Huawei Technologies Co., Ltd.
# This file is a part of the CANN Open Software.
# Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# ======================================================================================================================

cmake_minimum_required(VERSION 3.16.0)
project(ascendc-api-adv)

option(BUILD_OPEN_PROJECT "Build open ascendc lib project." ON)

set(ASCENDC_API_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(ASCENDC_SCRIPTS_DIR ${ASCENDC_API_DIR}/cmake/scripts)

if(BUILD_OPEN_PROJECT)
    include(cmake/config.cmake)
    set(INSTALL_LIBRARY_DIR packages/${CMAKE_SYSTEM_PROCESSOR}-linux)
else()
    set(INSTALL_LIBRARY_DIR lib)
endif()

set(GEN_KERENL_TILING_DATA_SCRIPT ${ASCENDC_SCRIPTS_DIR}/gen_kernel_tiling_data_def.py)
set(TILING_DATA_DEF_DIR ${ASCENDC_API_DIR}/lib)
set(KERNEL_TILING_DIR   ${CMAKE_CURRENT_BINARY_DIR})
set(KERNEL_TILING_HAED  ${KERNEL_TILING_DIR}/kernel_tiling/kernel_tiling.h)
set(PKG_KERNEL_TILING_DIR ${ASCEND_CANN_PACKAGE_PATH}/${CMAKE_SYSTEM_PROCESSOR}-linux/ascendc/include/highlevel_api/lib)

add_custom_command(OUTPUT ${KERNEL_TILING_HAED}
    COMMAND ${HI_PYTHON}  ${GEN_KERENL_TILING_DATA_SCRIPT} ${TILING_DATA_DEF_DIR} ${PKG_KERNEL_TILING_DIR} ${KERNEL_TILING_HAED}
    DEPENDS ${GEN_KERENL_TILING_DATA_SCRIPT})

add_custom_target(gen_kernel_tiling ALL
    DEPENDS ${KERNEL_TILING_HAED})

add_library(kernel_tiling_headers INTERFACE)
add_dependencies(kernel_tiling_headers gen_kernel_tiling)
target_include_directories(kernel_tiling_headers INTERFACE
    $<INSTALL_INTERFACE:include>
    $<INSTALL_INTERFACE:include/tikcpp>
    $<INSTALL_INTERFACE:include/tikcpp/tikcfw>
    $<INSTALL_INTERFACE:include/tikcpp/tikcfw/kernel_tiling>
    $<BUILD_INTERFACE:${KERNEL_TILING_DIR}>
)

add_subdirectory(impl)
if(ENABLE_TEST)
    add_subdirectory(tests)
endif()

install(FILES
    ${KERNEL_TILING_HAED}
    DESTINATION ${INSTALL_LIBRARY_DIR}/ascendc/include/highlevel_api/kernel_tiling/ OPTIONAL
)

install(DIRECTORY lib
    DESTINATION ${INSTALL_LIBRARY_DIR}/ascendc/include/highlevel_api OPTIONAL
)

install(DIRECTORY impl
    DESTINATION ${INSTALL_LIBRARY_DIR}/ascendc/include/highlevel_api OPTIONAL
    FILES_MATCHING
    PATTERN "*.h"
    PATTERN "CMakeLists.txt" EXCLUDE
)

configure_file(
    ${ASCENDC_API_DIR}/cmake/kernel_headers.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/kernel_headers.cmake
    @ONLY
)
install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/kernel_headers.cmake)

if(NOT BUILD_OPEN_PROJECT)
    install_package(
        PACKAGE kernel_tiling
        TARGETS kernel_tiling_headers
        FILES ${KERNEL_TILING_HAED}
        DESTINATION ${INSTALL_INCLUDE_DIR}/ascendc/include/highlevel_api/kernel_tiling
    )

    install(SCRIPT ${ASCENDC_API_DIR}/cmake/kernel_directory.cmake)
endif()
